﻿using Artech.Common;
using Artech.Core.Caching;
using Artech.Core.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace Artech.Core.Resources.Configuration
{
    public class ResourceProviderDataBase: ProviderDataBase<IResourceProvider>
    {
        public ResourceProviderDataBase() { }
        public ResourceProviderDataBase(Type type)
            : base(null, type) { }
        public ResourceProviderDataBase(string name, Type type)
            : base(name, type) { }

        [ConfigurationProperty(Constants.ConfigurationProperties.CacheStore, IsRequired = false, DefaultValue = null)]
        public StringResourceCacheStoreData CacheStore
        {
            get { return (StringResourceCacheStoreData)base[Constants.ConfigurationProperties.CacheStore]; }
            set { base[Constants.ConfigurationProperties.CacheStore] = value; }
        }

        protected virtual Func<ICacheStore<IEnumerable<StringResourceItem>>> CacheStoreAccessor
        {
            get
            {
                if (null == this.CacheStore)
                {
                    return () => null;
                }
                return () => new DefaultCacheStore<IEnumerable<StringResourceItem>>(Constants.CacheStorePrefixes.Resources,
                    this.CacheStore.CacheManager, this.CacheStore.Priority, this.CacheStore.ExpirationMode, this.CacheStore.ExpiredTime, null);
            }
        }

        protected virtual bool CacheEnabled
        {
            get { return null != this.CacheStore && this.CacheStore.Enabled; }
        }

        protected virtual CacheGranularity CacheGranularity
        {
            get { return this.CacheEnabled ? this.CacheStore.CacheGranularity : CacheGranularity.Category; }
        }
    }
}
